package com.heifeng.cloudshelf.mvp.search.adapter;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.heifeng.cloudshelf.base.MyApp;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.mvp.model.DetailImageInfo;
import com.heifeng.cloudshelf.mvp.search.activity.SearchHouseActivity;
import com.heifeng.cloudshelf.utils.DensityUtils;

import java.util.List;

/**
 * Created by admin on 2019/9/12.
 */

public class SearchHomeRvAdapter extends BaseQuickAdapter<DetailImageInfo, BaseViewHolder> {
    private Context context;

//    private Bitmap bitmap;
//
//    private Bitmap bitmap1;

    private int imageWidht = 500;
    private int imageHeight = 350;

    public SearchHomeRvAdapter(Context context, int layoutResId, @Nullable List<DetailImageInfo> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, DetailImageInfo item) {

        LinearLayout.LayoutParams layoutParamsTop = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsTop.height = MyApp.SCREEN_WIDTH / 3;
        layoutParamsTop.width = (MyApp.SCREEN_HEIGHT - DensityUtils.dp2px(context, 130)) / 5;
        layoutParamsTop.setMargins(0, DensityUtils.dp2px(context, 18), DensityUtils.dp2px(context, 20), 0);

        LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsBottom.height = MyApp.SCREEN_WIDTH / 3;
        layoutParamsBottom.width = (MyApp.SCREEN_HEIGHT - DensityUtils.dp2px(context, 130)) / 5;
        layoutParamsBottom.setMargins(0, DensityUtils.dp2px(context, 18), DensityUtils.dp2px(context, 20), 0);

        helper.getView(R.id.l_top).setLayoutParams(layoutParamsTop);
        helper.getView(R.id.l_bottom).setLayoutParams(layoutParamsBottom);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.height = MyApp.SCREEN_WIDTH / 3 - DensityUtils.dp2px(context, 50);
        //layoutParams.width = (App.SCREEN_HEIGHT - DensityUtils.dp2px(context, 130)) / 5;
        helper.getView(R.id.iv_top).setLayoutParams(layoutParams);
        helper.getView(R.id.iv_bottom).setLayoutParams(layoutParams);

        RoundedCorners roundedCorners = new RoundedCorners(2);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
//        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//        options.skipMemoryCache(true);

        if (null != item.getFirstImage() && !TextUtils.isEmpty(item.getFirstImage().getImage())) {
            RequestOptions options = new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE).override(imageWidht,imageHeight).centerCrop();
            Glide.with(mContext).load(item.getFirstImage().getImage()).apply(options).into((ImageView) helper.getView(R.id.iv_top));
//            Glide.with(mContext).asBitmap().load(item.getFirstImage().getImage()).apply(options).into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
//                    int resourceWidth = resource.getWidth();
//                    int resourceHeight = resource.getHeight();
//
//                    int targetWidth = resource.getWidth();
//                    int targetHeight = resource.getHeight();
//
//                    //计算缩放比例
//                    float sy;
//
//                    if (resourceHeight > resourceWidth) {////竖图
//                        targetWidth = resourceWidth;
//                        sy = (float) (targetWidth * 0.1) / (float) (resourceWidth * 0.1);
//                        targetHeight = (int) (resourceHeight * sy);
//                    } else if (resourceHeight == resourceWidth) {
//
//                    } else {//横图
//                        targetHeight = resourceHeight;
//                        sy = (float) (targetHeight * 0.1) / (float) (resourceHeight * 0.1);
//                        targetWidth = (int) (resourceWidth * sy);
//                    }
//
//
//                    bitmap = null;
//                    if (resource != null) {
//                        bitmap = Bitmap.createBitmap(resource, 0, 0, targetWidth, targetHeight);
//                    }
//
//
//                    ((ImageView) helper.getView(R.id.iv_top)).setImageBitmap(bitmap);
//
//                    if (bitmap != null && !bitmap.isRecycled()) {
//                        //bitmap.recycle();
//                        bitmap = null;
//                    }
//                }
//            });

            helper.getView(R.id.l_top).setVisibility(View.VISIBLE);

            helper.setText(R.id.tv_top_title, item.getFirstImage().getName());
            helper.setText(R.id.tv_top_address, item.getFirstImage().getAddress());

            helper.getView(R.id.tv_top_title).setVisibility(View.VISIBLE);
            helper.getView(R.id.tv_top_address).setVisibility(View.VISIBLE);

            helper.getView(R.id.iv_top).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("adapter", "tiem name:" + item.getFirstImage().getName());
                    openDetail((String) item.getFirstImage().getObj(), item.getFirstImage().getName());
                }
            });
        } else {
            helper.getView(R.id.l_top).setVisibility(View.INVISIBLE);
            helper.getView(R.id.tv_top_title).setVisibility(View.INVISIBLE);
            helper.getView(R.id.tv_top_address).setVisibility(View.INVISIBLE);

        }

//        Log.e("search","item.getSecondImage():"+item.getSecondImage().toString());
        if (null != item.getSecondImage() && !TextUtils.isEmpty(item.getSecondImage().getImage())) {
            RequestOptions options = new RequestOptions().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.RESOURCE).override(imageWidht,imageHeight).centerCrop();
            Glide.with(mContext).load(item.getSecondImage().getImage()).apply(options).into((ImageView) helper.getView(R.id.iv_bottom));
//            Glide.with(mContext).asBitmap().load(item.getSecondImage().getImage()).apply(options).into(new SimpleTarget<Bitmap>() {
//                @Override
//                public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
//                    int resourceWidth = resource.getWidth();
//                    int resourceHeight = resource.getHeight();
//
//                    int targetWidth = resource.getWidth();
//                    int targetHeight = resource.getHeight();
//
//                    //计算缩放比例
//                    float sy;
//
//                    if (resourceHeight > resourceWidth) {////竖图
//                        targetWidth = resourceWidth;
//                        sy = (float) (targetWidth * 0.1) / (float) (resourceWidth * 0.1);
//                        targetHeight = (int) (resourceHeight * sy);
//                    } else if (resourceHeight == resourceWidth) {
//
//                    } else {//横图
//                        targetHeight = resourceHeight;
//                        sy = (float) (targetHeight * 0.1) / (float) (resourceHeight * 0.1);
//                        targetWidth = (int) (resourceWidth * sy);
//                    }
//
//                    bitmap1 = null;
//                    if (resource != null) {
//                        bitmap1 = Bitmap.createBitmap(resource, 0, 0, targetWidth, targetHeight);
//                    }
//
//
//                    ((ImageView) helper.getView(R.id.iv_bottom)).setImageBitmap(bitmap1);
//
//                    if (bitmap1 != null && !bitmap1.isRecycled()) {
//                        //bitmap.recycle();
//                        bitmap1 = null;
//                    }
//                }
//            });

            helper.getView(R.id.l_bottom).setVisibility(View.VISIBLE);

            helper.setText(R.id.tv_bottom_title, item.getSecondImage().getName());
            helper.setText(R.id.tv_bottom_address, item.getSecondImage().getAddress());

            helper.getView(R.id.tv_bottom_title).setVisibility(View.VISIBLE);
            helper.getView(R.id.tv_bottom_address).setVisibility(View.VISIBLE);
            helper.getView(R.id.iv_bottom).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.e("adapter", "tiem name:" + item.getSecondImage().getName());
                    openDetail((String) item.getSecondImage().getObj(), item.getSecondImage().getName());
                }
            });
        } else {
            helper.getView(R.id.l_bottom).setVisibility(View.INVISIBLE);
            helper.getView(R.id.tv_bottom_title).setVisibility(View.INVISIBLE);
            helper.getView(R.id.tv_bottom_address).setVisibility(View.INVISIBLE);
        }

    }

    public void openDetail(String id, String title) {
        SearchHouseActivity.open(context, id, title);
    }
}
